package com.carrot.api.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.carrot.common.annotation.Log;
import com.carrot.common.core.controller.BaseController;
import com.carrot.common.core.domain.AjaxResult;
import com.carrot.common.core.domain.dto.PointDataDto;
import com.carrot.common.core.domain.dto.SysStationDto;
import com.carrot.common.core.domain.entity.SysStation;
import com.carrot.common.core.domain.vo.SysStationVo;
import com.carrot.common.enums.BusinessType;
import com.carrot.common.exception.ServiceException;
import com.carrot.common.mybatis.Page;
import com.carrot.common.utils.StringUtils;
import com.carrot.common.utils.poi.ExcelUtil;
import com.carrot.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 场站信息
 *
 * @author liuhongzhen
 */
@RestController
@RequestMapping("/system/station")
public class SysStationController extends BaseController {

    @Autowired
    private ISysStationService stationService;

    @Autowired
    private IStationPointService stationPointService;

    /**
     * 获取场站列表
     */
    @GetMapping("/list")
    public AjaxResult list(SysStationDto dto) {

        Page<SysStation, SysStationVo> page = new Page<>(dto.getPageNum(), dto.getPageSize());

        QueryWrapper<SysStation> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotEmpty(dto.getSearchContent())) {
            queryWrapper.like("station_id", dto.getSearchContent())
                    .or()
                    .like("station_name", dto.getSearchContent());
        }

        if (StringUtils.isNotEmpty(dto.getEnable())){
          queryWrapper.eq("enable",dto.getEnable());
        }

        queryWrapper.orderByAsc("create_time");
        page.setQueryWrapper(queryWrapper);
        page = stationService.findPage(page);

        return AjaxResult.success(page.getPageData());
    }

    /**
     * 根据场站编号获取详细信息
     */
    @GetMapping("detail")
    public AjaxResult getInfo(@RequestParam(value = "id") Long id) {
        SysStation station = stationService.getById(id);
        return AjaxResult.success(station);
    }

    /**
     * 新增场站
     */
    @Log(title = "场站管理", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@Validated @RequestBody SysStation station) {
        if (StringUtils.isAnyEmpty(station.getStationId(), station.getStationName(), station.getStationAlias())) {
            throw new ServiceException("参数校验失败");
        }
        if (null == station.getClusterNum() || station.getClusterNum() <= 0) {
            throw new ServiceException("电池簇的个数不能小于0");
        }

        if (null == station.getPackNum() || station.getPackNum() <= 0) {
            throw new ServiceException("电池组的个数不能小于0");
        }
        if (null == station.getCellNum() || station.getCellNum() <= 0) {
            throw new ServiceException("单体的个数不能小于0");
        }

        int packCellNum = station.getCellNum() % station.getPackNum();
        if (packCellNum != 0) {
            throw new ServiceException("单体的个数必须是电池组的整倍数");
        }

        station.setCreateBy(getUsername());
        return toAjax(stationService.insertStation(station));
    }

    /**
     * 修改场站
     */
    @Log(title = "场站管理", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@Validated @RequestBody SysStation station) {
        station.setUpdateBy(getUsername());
        return toAjax(stationService.updateStation(station));
    }

    /**
     * 删除场站
     */
    @Log(title = "场站管理", businessType = BusinessType.DELETE)
    @GetMapping("remove")
    public AjaxResult remove(@RequestParam(value = "id") Long id) {
        return toAjax(stationService.deleteStationById(id));
    }

    /**
     * 删除场站
     */
    @Log(title = "场站管理", businessType = BusinessType.DELETE)
    @GetMapping("batchRemove")
    public AjaxResult batchRemove(@RequestBody SysStationDto dto) {
        return toAjax(stationService.deleteStationByIds(dto.getIds()));
    }

    /**
     * 更改状态
     */
    @PostMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SysStationDto dto) {
        if (null == dto.getId() || StringUtils.isEmpty(dto.getEnable())){
            throw new ServiceException("参数校验失败");
        }
        stationService.changeStatus(dto);
        return AjaxResult.success();
    }

    /**
     * 点表导入
     *
     * @param file
     * @return
     * @throws Exception
     */
    @Log(title = "点表配置", businessType = BusinessType.IMPORT)
    @PostMapping("importPointData")
    public AjaxResult importPointData(MultipartFile file, String stationId) throws Exception {

        ExcelUtil<PointDataDto> util = new ExcelUtil<PointDataDto>(PointDataDto.class);

        List<PointDataDto> dataList = util.importExcel(file.getInputStream());

        String operateName = getUsername();
        String message = stationPointService.importPointData(stationId, dataList, operateName);

        return AjaxResult.success(message);
    }
}
